<?php
/**
 * 陈杰  18323491246
 */

namespace App\Http\Controllers;

use App\Models\FinancingListModel;
use App\Models\MemberWalletModel;
use App\Models\Money\PayOrderModel;
use App\Models\Money\WithdrawalModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MoneyController extends Controller
{

    public function statistics(Request $request)
    {
        error_reporting(0);
        $date = (int)$request->input('date');
        switch ($date) {
            case 2:
                $today   = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
                $between = $this->createBetween($today, 86400);
                $date    = $this->creatDate('m-d', $between, 86400);
                break;
            case 3: //按最近几个星期
                $w       = date('w') ? date('w') : 7;
                $toweek  = mktime(0, 0, 0, date('m'), date('d') - $w + 1, date('Y'));
                $between = $this->createBetween($toweek, 604800);
                $date    = $this->creatDate('m-d', $between, 604800);
                break;
            case 4: //按最近几个月
                $this->createBetweenMonth();
                $between = $this->createBetweenMonth();
                $date    = $this->creatDateMonth($between);
                break;

            default:
                $tohour  = mktime(date('H'), 0, 0, date('m'), date('d'), date('Y'));
                $between = $this->createBetween($tohour, 3600);
                $date    = $this->creatDate('H:i', $between, 3600);
                break;
        }
        foreach ($between as $k => $v) {
            $withdrawal[]      = DB::table('withdrawal')->whereBetween('update_time', $v)
                ->where('status', 1)->sum('money');
            $payOrder[]        = DB::table('pay_order')->whereBetween('return_time', $v)
                ->where('status', 1)->sum('money');
            $withdrawalCount[] = DB::table('withdrawal')->whereBetween('update_time', $v)
                ->where('status', 1)->count();
            $payOrderCount[]   = DB::table('pay_order')->whereBetween('return_time', $v)
                ->where('status', 1)->count();
        }
        $list['between']         = $between;
        $list['date']            = $date;
        $list['withdrawal']      = $withdrawal;
        $list['payOrder']        = $payOrder;
        $list['withdrawalCount'] = $withdrawalCount;
        $list['payOrderCount']   = $payOrderCount;
        return self::returnMsg($list);
    }

    /**
     * 充值记录
     */
    public function payOrderList(Request $request)
    {
        $firstWhere = function ($query) use ($request) {
            if ($request->has('status') and $request->status != '') {
                $query->where('pay_order.status', $request->status);
            }
            if ($request->has('date') and $request->date != '') {
                $start = strtotime($request->date[0]);
                $end   = strtotime($request->date[1]);
                $query->whereBetween('pay_order.create_time', [$start, $end]);
            }
        };
        $where      = function ($query) use ($request) {
            //筛选查询关键字
            if ($request->has('keywords') and $request->keywords != '') {
                $query->orWhere('member.name', '=', $request->keywords)
                    ->orWhere('pay_order.member_id', '=', $request->keywords)
                    ->orWhere('member.phone', '=', $request->keywords)
                    ->orWhere('pay_order.id', '=', $request->keywords)
                    ->orWhere('pay_order.order_no', '=', $request->keywords)
                    ->orWhere('pay_order.order_id', '=', $request->keywords);
            }
        };

        $data = PayOrderModel::where($firstWhere)->where($where)
            ->join('member', 'pay_order.member_id', '=', 'member.id')
            ->select('pay_order.*', 'member.phone as member_phone', 'member.name as member_name')
            ->paginate($request->get('count'));
        return self::returnMsg($data);
    }

    public function withdrawalList(Request $request)
    {
        $firstWhere = function ($query) use ($request) {
            if ($request->has('status') and $request->status != '') {
                $query->where('withdrawal.status', $request->status);
            }
            if ($request->has('date') and $request->date != '') {
                $start = strtotime($request->date[0]);
                $end   = strtotime($request->date[1]);
                $query->whereBetween('withdrawal.create_time', [$start, $end]);
            }
        };
        $where      = function ($query) use ($request) {
            //筛选查询关键字
            if ($request->has('keywords') and $request->keywords != '') {
                $query->orWhere('member.name', '=', $request->keywords)
                    ->orWhere('withdrawal.member_id', '=', $request->keywords)
                    ->orWhere('member.phone', '=', $request->keywords)
                    ->orWhere('withdrawal.id', '=', $request->keywords);
            }
        };

        $data = WithdrawalModel::with('payment')
            ->where($firstWhere)->where($where)
            ->join('member', 'withdrawal.member_id', '=', 'member.id')
            //->join('member_payment', 'withdrawal.payment_id', '=', 'member_payment.id')
            ->select('withdrawal.*', 'member.phone as member_phone', 'member.name as member_name')
            ->paginate($request->get('count'));
        return self::returnMsg($data);
    }

    public function withdrawalEdit(Request $request)
    {
        $rules      = [
            'id'     => 'required|integer|min:1',
            'status' => 'required|integer|in:1,2',
            'remark' => 'max:255'
        ];
        $sqlmap     = self::checkValidate($rules, $request->all());
        $withdrawal = WithdrawalModel::find($sqlmap['id']);
        //dd($withdrawal);
        if (!isset($withdrawal) or $withdrawal->status != 0)
            return self::errorMsg('没有找到该提现申请');
        try {
            DB::beginTransaction();
            if ($sqlmap['status'] == 1) {
                //处理申请
                $withdrawal->status = 1;
            } else {
                //驳回申请
                $withdrawal->status    = 2;
                $member_wallet         = MemberWalletModel::where('member_id', $withdrawal->member_id)->first();
                $member_wallet->profit += $withdrawal->money;
                FinancingListModel::write_profit($withdrawal->member_id, $withdrawal->money,
                    $member_wallet->profit, 'admin_withdrawal_reject', '后台驳回提现申请');
                $member_wallet->save();
            }
            $withdrawal->remark      = isset($sqlmap['remark']) ? $sqlmap['remark'] : $withdrawal->remark;
            $withdrawal->update_time = time();
            $withdrawal->save();
            DB::commit();
            return self::successMsg();
        } catch (\Exception $exception) {
            DB::rollBack();
            return self::errorMsg($exception->getMessage());
        }
    }
}
